package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.ProductTypeModel;
import com.zeng.jiudian.dao.ProductTypeDao;
import com.zeng.jiudian.dao.impl.ProductTypeDaoImpl;
import com.zeng.jiudian.service.ProductTypeService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/8/12 - 11:05
 */
public class ProductTypeServiceImpl implements ProductTypeService
{
    @Override
    public int add(ProductTypeModel productTypeModel)
    {
        ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

        int count = productTypeDao.add(productTypeModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<ProductTypeModel> findAll(ProductTypeModel productTypeModel)
    {
        ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

        return productTypeDao.findAll(productTypeModel);
    }

    @Override
    public int getCount(ProductTypeModel productTypeModel)
    {
        ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

        return productTypeDao.getCount(productTypeModel);
    }

    @Override
    public ProductTypeModel findById(int id)
    {
        ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

        return productTypeDao.findById(id);
    }

    @Override
    public int delete(int id)
    {
        ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

        int count = productTypeDao.delete(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public int update(ProductTypeModel productTypeModel)
    {
        ProductTypeDao productTypeDao = new ProductTypeDaoImpl();

        if ("".equals(productTypeModel.getProductType()))
        {
            productTypeModel.setProductType(null);
        }
        if ("".equals(productTypeModel.getEnType()))
        {
            productTypeModel.setEnType(null);
        }

        int count = productTypeDao.update(productTypeModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<ProductTypeModel> getAll()
    {
        List<ProductTypeModel> list = null;
        Jedis jedis = new Jedis();
        jedis.select(0);
        Long length = jedis.llen("productTypeId");

        if (length > 0)
        {
            list = new ArrayList<>();
            List<String> productTypeId = jedis.lrange("productTypeId", 0, length);
            List<String> productTypeName = jedis.lrange("productTypeName", 0, length);
            List<String> productTypeEnTypeName = jedis.lrange("productTypeEnTypeName", 0, length);
            List<String> productTypeIsShow = jedis.lrange("productTypeIsShow", 0, length);
            List<String> productTypeRecommend = jedis.lrange("productTypeRecommend", 0, length);
            List<String> productTypeCreateTime = jedis.lrange("productTypeCreateTime", 0, length);
            List<String> productTypeUpdateTime = jedis.lrange("productTypeUpdateTime", 0, length);
            for (int i = 0; i < length; i++)
            {
                ProductTypeModel model = new ProductTypeModel();
                model.setId(Integer.parseInt(productTypeId.get(i)));
                model.setProductType(productTypeName.get(i));
                model.setEnType(productTypeEnTypeName.get(i));
                model.setIsShow(Integer.parseInt(productTypeIsShow.get(i)));
                model.setRecommend(Integer.parseInt(productTypeRecommend.get(i)));
                model.setCreateTime(productTypeCreateTime.get(i));
                model.setUpdateTime(productTypeUpdateTime.get(i));
                list.add(model);
            }
        }
        else
        {
            ProductTypeDao productTypeDao = new ProductTypeDaoImpl();
            list = productTypeDao.getAll();
            jedis.del("productTypeId", "productTypeName", "productTypeEnTypeName", "productTypeIsShow", "productTypeRecommend", "productTypeCreateTime", "productTypeUpdateTime");
            for (ProductTypeModel model : list)
            {
                jedis.rpush("productTypeId", model.getId().toString());
                jedis.rpush("productTypeName", model.getProductType());
                jedis.rpush("productTypeEnTypeName", model.getEnType());
                jedis.rpush("productTypeIsShow", model.getIsShow().toString());
                jedis.rpush("productTypeRecommend", model.getRecommend().toString());
                jedis.rpush("productTypeCreateTime", model.getCreateTime());
                jedis.rpush("productTypeUpdateTime", model.getUpdateTime());
            }
        }

        return list;
    }
}
